<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of uploadfile
 *
 * @author miyzan
 */
class uploadfile extends MY_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
    }

    public function index() {
//        move_uploaded_file($_FILES["photo-path"]["tmp_name"], "assets/images/" . $_FILES["photo-path"]["name"]);

        $returnResponse = $_REQUEST['returnResponse'];
        sleep(1);
        if ($returnResponse != "") {
            header('HTTP/1.0 ' . $returnResponse . ' Server status', true, $returnResponse);
            echo '{success:false, message:"Faked error from server", errors:{"photo-path":"The server returned this"}}';
        } else {
            $file = $_FILES['photo-path'];
            $fileName = $file['name'];
            $fileSize = $file['size'];
            if (!$fileSize) {
                $fileSize = $_SERVER['CONTENT_LENGTH'];
            }
            move_uploaded_file($_FILES["photo-path"]["tmp_name"], "assets/images/" . $_FILES["photo-path"]["name"]);

            echo json_encode(array(
                "success" => true,
                "fileName" => $fileName,
                "fileSize" => $fileSize
            ));
        }
    }
    public function update_rows() {
        
    }

    public function upload_proses() {
        $allowedExts = array(
            "gif",
            "jpeg",
            "jpg",
            "png"
        );
        $temp = explode(".", $_FILES["photo-path"]["name"]);
        $extension = end($temp);

        $infoString = "";
        $file = $_FILES['photo-path'];
        $fileName = $file['name'];
        $fileSize = $file['size'];
       
        if ((($_FILES["photo-path"]["type"] == "image/gif") || ($_FILES["photo-path"]["type"] == "image/jpeg") || ($_FILES["photo-path"]["type"] == "image/jpg") || ($_FILES["photo-path"]["type"] == "image/pjpeg") || ($_FILES["photo-path"]["type"] == "image/x-png") || ($_FILES["photo-path"]["type"] == "image/png")) && ($_FILES["photo-path"]["size"] < 9999999)
//&& ($_FILES["photo-path"]["size"] < 20000) 
                && in_array($extension, $allowedExts)) {
            if ($_FILES["photo-path"]["error"] > 0) {
                die("{'success': false, 'msg': '" . $_FILES["photo-path"]["error"] . "'}");
            } else {
                $infoString .= " Upload: " . $_FILES["photo-path"]["name"] . "<br>";
                $infoString .= " Type: " . $_FILES["photo-path"]["type"] . "<br>";
                $infoString .= " Size: " . ($_FILES["photo-path"]["size"] / 1024) . " kB<br>";
                $infoString .= " Temp file: " . $_FILES["photo-path"]["tmp_name"] . "<br>";

                if (file_exists("assets/images/" . $_FILES["photo-path"]["name"])) {
                    die("{'success': false, 'msg': '" . $_FILES["photo-path"]["name"] . " already exists.'}");
                } else {
                    move_uploaded_file($_FILES["photo-path"]["tmp_name"], "assets/images/" . $_FILES["photo-path"]["name"]);
                    $infoString .= " Stored in: " . "assets/images/" . $_FILES["photo-path"]["name"];
                    echo json_encode(array(
                        "success" => true,
                        "fileName" => $fileName,
                        "fileSize" => $fileSize,
                        "msg" => 'Upload Success Filename : ' . $fileName . ' Filesize : ' . $fileSize
                    ));
                }
            }
        } else {
//            die("{'success': false, 'error': 'File too big or invalid!'}");
            echo json_encode(array(
                    "success" => false,
                    "fileName" => $fileName,
                    "fileSize" => $fileSize,
                    "msg" =>'File too big or invalid!'
                ));
        }

        
//        echo "{'success': true, 'updateInfo': '" . $infoString . "'}";
    }

}

?>
